<template>
  <div>
    <!-- 底部导航 -->
    <div class="flex_cc bomttomNav">
      <div
        class="item userCenter"
        v-for="(item, index) in bomttomNav"
        :key="index"
        @click="jumpRoute(item.route)"
      >
        <div class="backgroundImageBase icon" :style="'background-image: url(' + item.icon + ')'"></div>
        <div class="name">{{ item.name }}</div>
      </div>
    </div>
    <!-- 轮播图 -->
    <swiper class="banner" ref="mySwiper" :options="swiperOptions">
      <swiper-slide
        class="backgroundImageBase item"
        v-for="(item, index) in banner"
        :key="index"
        :style="'background-image: url(' + item + ')'"
      ></swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
    <!-- 服务项目 -->
    <div class="flex_cc serviceItems">
      <div
        class="item"
        v-for="(item, index) in serviceItems"
        :key="index"
        @click="jumpRoute(item.route)"
      >
        <div class="backgroundImageBase icon" :style="'background-image: url(' + item.icon + ')'"></div>
        <div class="content">
          <div class="title">{{ item.title }}</div>
          <div class="tips">{{ item.tips }}</div>
        </div>
      </div>
    </div>
    <!--   绑定手机号   -->
    <van-popup v-model="noPhone" :close-on-click-overlay="false">
      <div class="index-phone-form">
        <div class="title">绑定手机号码</div>
        <van-field v-model="tel" type="tel" placeholder="请输入手机号" />
        <van-field v-model="sms" center clearable placeholder="请输入短信验证码">
          <template #button>
            <van-button
              size="small"
              v-show="!clicked"
              @click="getsms(countTime)"
              type="primary"
            >获取验证码</van-button>
            <van-button
              size="small"
              v-show="clicked"
              @click="showTips()"
              color="#aaa"
            >{{ countTime }}秒后再次获取</van-button>
          </template>
        </van-field>
        <div class="flex_ button-box">
          <van-button type="danger" @click="cancelPhone()">取消</van-button>
          <van-button type="primary" @click="confirmPhone()">确认</van-button>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
export default {
  props: {
    banner: Array,
    routes: Object,
    user: Object,
    bind: String
  },
  data() {
    return {
      noPhone: false,
      tel: "",
      sms: "",
      countTime: 60,
      clicked: false,
      swiperOptions: {
        autoplay: true,
        loop: true,
        pagination: {
          el: ".swiper-pagination"
        }
      },
      bomttomNav: [
        {
          icon: require("./../static/icon_orders.png"),
          name: "我的订单"
        },
        {
          icon: require("./../static/icon_user.png"),
          name: "个人中心"
        }
      ],
      serviceItems: [
        {
          icon: require("./../static/icon_send.png"),
          title: "帮我送",
          tips: "文件/鲜花/钥匙"
        },
        {
          icon: require("./../static/icon_buy.png"),
          title: "帮我买",
          tips: "啤酒/香烟/矿泉水"
        },
        {
          icon: require("./../static/icon_get.png"),
          title: "帮我取",
          tips: "衣服/快递/发票"
        },
        {
          icon: require("./../static/icon_other.png"),
          title: "其他服务",
          tips: "大件货物/帮忙办事"
        }
      ]
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper;
    }
  },
  mounted() {
    this.routesData();
    console.log(this.user);
  },
  methods: {
    routesData() {
      console.log(this.routes);
      this.serviceItems[0].route = [this.routes.send];
      this.bomttomNav[1].route = this.routes.user_center;
      console.log(this.serviceItems);
    },
    jumpRoute(route) {
      const havePhone = this.user.phone;
      if (havePhone === "" || havePhone === null) {
        this.noPhone = true;
      } else {
        location.href = route;
      }
    },
    showTips() {
      this.$toast("已经点过了，请耐心等待。");
    },
    getsms(second) {
      let that = this;
      axios
        .get("apply/sendUser", { params: { phone: that.tel } })
        .then(res => {
          console.log(res);
          if (res.data.status === 200) {
            this.$toast(res.data.msg);
          }
        })
        .catch(err => {
          console.log(err.response);
          if (err.response.data.hasOwnProperty("errors")) {
            let obj = err.response.data.errors;
            for (let key in obj) {
              for (let key2 in obj[key]) {
                // console.log(obj[key][0]);
                this.$toast(obj[key][0]);
                return false;
              }
              return false;
            }
          } else {
            this.$toast(err.response.data.msg);
            return false;
          }
        });

      that.clicked = true;
      const countdownSet = setInterval(() => {
        second--;
        that.countTime = second;
        if (second <= 0) {
          that.clicked = false;
          clearInterval(countdownSet);
        }
      }, 1000);
    },
    cancelPhone() {
      this.noPhone = false;
      this.tel = "";
      this.sms = "";
    },
    confirmPhone() {
      let that = this;
      const phoneReg = /^[1][3,4,5,7,8][0-9]{9}$/;
      // if (this.tel == ''){
      //     this.$toast('手机号码不能为空');
      //     return false
      // } else if ( !phoneReg.test(this.tel) ){
      //     this.$toast('手机号码格式不正确');
      //     return false
      // }
      /*if (this.sms == ''){
                this.$toast('验证码不能为空');
                return false
            }*/
      let data = {
        phone: this.tel,
        yzm: this.sms
      };
      axios
        .post("user/bind", data)
        .then(res => {
          console.log(res);
          if (res.data.status === 200) {
            this.$toast(res.data.msg);
            that.noPhone = false;
            location.reload();
          }
        })
        .catch(err => {
          console.log(err);
          if (err.response.data.hasOwnProperty("errors")) {
            let obj = err.response.data.errors;
            for (let key in obj) {
              for (let key2 in obj[key]) {
                // console.log(obj[key][0]);
                this.$toast(obj[key][0]);
                return false;
              }
              return false;
            }
          } else {
            this.$toast(err.response.data.msg);
            return false;
          }
        });
    }
  }
};
</script>
